package com.hundsun.documentripper.config;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.error.BaseException;
import com.hundsun.documentripper.error.UnknownSessionException;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：全局异常处理
 * @date ：Created in 2021/5/17
 */
@ControllerAdvice
public class SpringExceptionHandler {

    /**
     * 全局处理UnknownSessionException
     *
     * @param e
     * @param webRequest
     * @return
     */
    @ExceptionHandler(value = {UnknownSessionException.class})
    public ResponseEntity<Object> handleOtherExceptions(final Exception e, final WebRequest webRequest) {
        return new ResponseEntity<Object>(
                BaseResponse.buildErrorResponse(((BaseException) e).getErrorCode()),
                HttpStatus.UNAUTHORIZED);
    }

}